package com.ry.project.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ry.project.demo.bean.Dept;
import com.ry.project.demo.service.DeptServiceImpl;
import com.ry.project.demo.service.IDeptService;

@Controller
@RequestMapping(value = "/demo")
public class DemoController {

	@Autowired
	private IDeptService deptService;
	
	@GetMapping
	public String demo() {
		return "/demo/index";
	}
	
	@RequestMapping(value = "/list")
	@ResponseBody
	public String list() {
		
		PageHelper.startPage(1, 3);
		List<Dept> list = deptService.getDataList();
		PageInfo<Dept> pageInfo = new PageInfo<Dept>(list);
        long total = pageInfo.getTotal(); //获取总记录数
		System.out.println(JSON.toJSONString(list));
		System.out.println(total);
		
		return JSON.toJSONString(list);
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String save() {
		Dept d = new Dept();
		d.setDeptid(190);
		d.setDname("测试添加");
		d.setLoc("cssw");
		int result = deptService.save(d);
		
		return String.valueOf(result);
	}
	
	@RequestMapping(value = "/test")
	@ResponseBody
	public String testT() {
		Dept d = new Dept();
		d.setDeptid(190);
		d.setDname("测试事务");
		d.setLoc("cssw");
		int result = deptService.testTransactional(d, 20);
		
		return String.valueOf(result);
	}
	
	
}
